package com.hk.web.manage.login.controller;

import com.hk.commons.annotation.IPLimiting;
import com.hk.commons.base.BaseController;
import com.hk.commons.eunms.SysCode;
import com.hk.commons.utils.BeanConvertUtil;
import com.hk.commons.vo.JsonResult;
import com.hk.manage.login.bo.LoginBO;
import com.hk.manage.login.bo.ManagerRegisterBO;
import com.hk.manage.login.service.LoginService;
import com.hk.web.manage.login.dto.LoginDTO;
import com.hk.web.manage.login.dto.ManagerRegisterDTO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@RestController
public class ManagerLoginController extends BaseController {

    @Resource
    private LoginService loginService;

    /**
     * /login
     */
    @IPLimiting
    @PostMapping("/login")
    public JsonResult managerLogin(@Valid LoginDTO dto) {
        LoginBO bo = BeanConvertUtil.copyObj(dto, LoginBO.class);
        return copyToJsonResultNoData(loginService.loginUpdate(bo));
    }

    /**
     * /logout
     */
    @GetMapping("/logout")
    public JsonResult managerLogout() {
        Subject subject = SecurityUtils.getSubject();
        if (null != subject){
            subject.getSession().stop();
            if (subject.isAuthenticated()){
                subject.logout();
            }
        }

        return new JsonResult();
    }

    /**
     * /register
     */
    @PostMapping("/register")
    public JsonResult register(ManagerRegisterDTO dto) {
        ManagerRegisterBO bo = BeanConvertUtil.copyObj(dto, ManagerRegisterBO.class);
        return copyToJsonResultNoData(loginService.registerInsert(bo));
    }

}
